package com.rk.admin.controller;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.Comarticle;
import com.rk.admin.domain.Commonarticle;
import com.rk.admin.domain.Particle;
import com.rk.admin.domain.Resultarticle;
import com.rk.admin.service.ComarticleService;
import com.rk.admin.service.ParticleService;
import com.rk.admin.service.ResultService;
import com.rk.admin.tools.CommonResult;
import com.rk.admin.tools.SysConstant;
import com.rk.admin.utils.FileNameUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



@Api(tags = "添加所有模块文章")
@RestController
@CrossOrigin //前后端分离配置跨域访问
@RequestMapping("/add")
public class AddarticleControllerl {


    @Value("${environment.system}")
    private String system;


    @Autowired
    private ComarticleService comarticleService;

    @Autowired
    private ParticleService particleService;

    @Autowired
    private ResultService resultService;

    /**
     * 添加文章
     * @param commonarticle
     * @return
     */
    @ApiOperation("添加文章")
    @PostMapping("/Article")
    public CommonResult add(@RequestBody Commonarticle commonarticle) {
        Boolean flag = false;
        //如果是合作交流模块的文章
        if(commonarticle.getModel().equals("comartic"))//如果是合作交流的
        {
            Comarticle comarticle=new Comarticle();
            comarticle.setTitle(commonarticle.getTitle());
            comarticle.setContent(commonarticle.getContent());
            comarticle.setMdcontent(commonarticle.getMdcontent());
            flag = comarticleService.add(comarticle);
        }
        //添加人才培养本科生文章
        if(commonarticle.getModel().equals("particleben"))
        {
            Particle particle=new Particle();
            particle.setTitle(commonarticle.getTitle());
            particle.setContent(commonarticle.getContent());
            particle.setMdcontent(commonarticle.getMdcontent());
            flag=particleService.addben(particle);
        }
        //添加人才培养研究生文章
        if(commonarticle.getModel().equals("particleyan"))
        {
            Particle particle=new Particle();
            particle.setTitle(commonarticle.getTitle());
            particle.setContent(commonarticle.getContent());
            particle.setMdcontent(commonarticle.getMdcontent());
            flag=particleService.addyan(particle);
        }
        //添加项目成果文章
        if(commonarticle.getModel().equals("resultarticle"))
        {
            Resultarticle resultarticle=new Resultarticle();
            resultarticle.setTitle(commonarticle.getTitle());
            resultarticle.setContent(commonarticle.getContent());
            resultarticle.setMdcontent(commonarticle.getMdcontent());
            flag=resultService.add(resultarticle);
        }

        if(flag){
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("添加失败!!");
        }
    }


    @ApiOperation("文章上传图片")
    @PostMapping("/uploadimg")
    public CommonResult uploadImg(HttpServletRequest req, MultipartFile image) {

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        StringBuffer url = new StringBuffer();

        String filePath="";
        //  windows环境下    获取项目根路径
        String path = System.getProperty("user.dir");

        //文件上传的真实路径
        if("linux".equals(system)){
            filePath= SysConstant.ARTICLEiMG_LINUX;
        }else if("windows".equals(system)){
            filePath = path+ SysConstant.ARTICLEiMG_WINDOWS;
        }


        System.out.println("文章图片上传目录:"+filePath);

        File imgFolder = new File(filePath);
        if (!imgFolder.exists()) {
            imgFolder.mkdirs();
        }

        url.append(req.getScheme())
                .append("://")
                .append(req.getServerName()) //拼接ip
                .append(":")
                .append(req.getServerPort())  //拼接端口
                .append("/articImg");

        //获取上传的文件名
        String originalFilename = image.getOriginalFilename();
        //获取文件类型
        String fileType = FileNameUtil.getFileType(originalFilename);
        String imgName = FileNameUtil.getUUIDFileName() + fileType;
        try {
             IOUtils.write(image.getBytes(), new FileOutputStream(new File(imgFolder, imgName)));
             url.append("/").append(imgName);
             System.out.println("图片url:"+url.toString());
             return  CommonResult.success(url.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed("上传失败");
    }

}
